package com.tools.developer.util;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import android.text.TextUtils;

public class FileUtil {

	public static boolean mkDirs(String path) {
		if (TextUtils.isEmpty(path)) {
			return false;
		}
		File file = new File(path);
		if (file != null && !file.exists()) {
			return file.mkdirs();
		}
		return true;
	}

	private static long lasttime = 0l;
	private static long lastlevel = 101;
	private static final long ONEMINUTE = 60 * 1000;
	private static boolean isDebug = false;

	// private static long lastlevelOnReceive = 101;
	// private static long lastlevelBig = 101;
	// private static long lastlevelSerUpdate = 101;
	// private static long lastLevelBatSerUpdate = 101;

	// public static void logWidgetOnReceive(String message, int level) {
	// if (lastlevelOnReceive != level) {
	// long curenttime = System.currentTimeMillis();
	// long mintues = (curenttime - lasttime )/ ONEMINUTE;
	//
	// StringBuilder sb = new StringBuilder();
	// sb.append("level: ").append(level);
	// sb.append(" " + message + " ");
	// if (lasttime != 0l) {
	// sb.append(" time: ").append(mintues).append(" mintues");
	// lasttime = System.currentTimeMillis();
	// }
	// sb.append("\r\n");
	// appendContent(Constant.KDB_LOG_PATH, "widget_battery_log.txt",
	// sb.toString());
	// lastlevelOnReceive = level;
	// }
	// }
	//
	// public static void logWidgetBig(String message, int level) {
	// if (lastlevelBig != level) {
	// long curenttime = System.currentTimeMillis();
	// long mintues = (curenttime - lasttime )/ ONEMINUTE;
	//
	// StringBuilder sb = new StringBuilder();
	// sb.append("level: ").append(level);
	// sb.append(" " + message + " ");
	// if (lasttime != 0l) {
	// sb.append(" time: ").append(mintues).append(" mintues");
	// lasttime = System.currentTimeMillis();
	// }
	// sb.append("\r\n");
	// appendContent(Constant.KDB_LOG_PATH, "widget_battery_log.txt",
	// sb.toString());
	// lastlevelBig = level;
	// }
	// }
	//
	// public static void logWidgetServiceUpdate(String message, int level) {
	// if (lastlevelSerUpdate != level) {
	// long curenttime = System.currentTimeMillis();
	// long mintues = (curenttime - lasttime )/ ONEMINUTE;
	//
	// StringBuilder sb = new StringBuilder();
	// sb.append("level: ").append(level);
	// sb.append(" " + message + " ");
	// if (lasttime != 0l) {
	// sb.append(" time: ").append(mintues).append(" mintues");
	// lasttime = System.currentTimeMillis();
	// }
	// sb.append("\r\n");
	// appendContent(Constant.KDB_LOG_PATH, "widget_battery_log.txt",
	// sb.toString());
	// lastlevelSerUpdate = level;
	// }
	// }
	//
	// public static void logBatteryServiceUpdate(String message, int level) {
	// if (lastLevelBatSerUpdate != level) {
	// long curenttime = System.currentTimeMillis();
	// long mintues = (curenttime - lasttime )/ ONEMINUTE;
	//
	// StringBuilder sb = new StringBuilder();
	// sb.append("level: ").append(level);
	// sb.append(" " + message + " ");
	// if (lasttime != 0l) {
	// sb.append(" time: ").append(mintues).append(" mintues");
	// lasttime = System.currentTimeMillis();
	// }
	// sb.append("\r\n");
	// appendContent(Constant.KDB_LOG_PATH, "widget_battery_log.txt",
	// sb.toString());
	// lastLevelBatSerUpdate = level;
	// }
	// }

	public static void appendContent(String path, String file, String string) {
		if (!isDebug) {
			return;
		}
		if (TextUtils.isEmpty(path) || TextUtils.isEmpty(file)
				|| TextUtils.isEmpty(string)) {
			return;
		}
		if (mkDirs(path)) {
			try {
				File f = new File(path + file);
				FileWriter writer = new FileWriter(f, true);
				writer.append(string);
				writer.close();
			} catch (Exception e) {
			}
			;
		}
	}

	public static void gzip(String srcPath, String srcfile, String decPath,
			String decFile) {
		if (TextUtils.isEmpty(srcPath) || TextUtils.isEmpty(srcfile)
				|| TextUtils.isEmpty(decPath)) {
			return;
		}
		try {
			File dec = new File(decPath + decFile);
			if (dec.exists()) {
				dec.delete();
			}
			File src = new File(srcPath + srcfile);
			if (!src.exists()) {
				return;
			}
			BufferedReader in = new BufferedReader(new InputStreamReader(
					new FileInputStream(src), "utf-8"));
			BufferedOutputStream out = new BufferedOutputStream(
					new GZIPOutputStream(new FileOutputStream(dec)));
			int c;
			while ((c = in.read()) != -1) {
				out.write(String.valueOf((char) c).getBytes("utf-8"));
			}
			in.close();
			out.close();
		} catch (Exception e) {
		}

	}

	public static boolean zipFiles(File fs[], File zf) {
		boolean result = false;

		try {
			int buff_len = 1024;
			byte buff[] = new byte[buff_len];

			ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(zf));
			for (int i = 0; i < fs.length; i++) {
				FileInputStream fi = new FileInputStream(fs[i]);
				ZipEntry ze = new ZipEntry(fs[i].getName());

				zo.putNextEntry(ze);
				while (true) {
					int count = fi.read(buff, 0, buff_len);
					if (count <= 0) {
						break;
					}

					zo.write(buff, 0, count);
				}

				fi.close();
			}

			zo.flush();
			zo.close();

			result = true;
		} catch (Exception e) {
		}

		return result;
	}

	public static boolean deleteFile(String path) {
		if (TextUtils.isEmpty(path)) {
			return false;
		}
		File file = new File(path);
		if (file.exists()) {
			return file.delete();
		}
		return true;
	}

	// add by zxy
	public static boolean IsSdcardExist() {
		String state = android.os.Environment.getExternalStorageState();
		if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {
			return true;
		}
		return false;
	}

	private static String getMIMEType(File file) {
		String type = "";
		String fName = file.getName();
		String end = fName
				.substring(fName.lastIndexOf(".") + 1, fName.length())
				.toLowerCase();
		if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
				|| end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
			type = "audio";
		} else if (end.equals("3gp") || end.equals("mp4")) {
			type = "video";
		} else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
				|| end.equals("jpeg") || end.equals("bmp")) {
			type = "image";
		} else if (end.equals("apk")) {
			type = "application/vnd.android.package-archive";
			// application/vnd.android.package-archive
		} else {
			type = "*";
		}
		if (end.equals("apk")) {
		} else {
			type += "/*";
		}
		return type;
	}

}